package org.indexador.util.stax;

import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.Date;

import javax.xml.stream.*;

import org.indexador.util.*;
import org.wikipedia.entidades.*;

/**
 * http://www.devx.com/Java/Article/30298/0/page/1 ->consulta
 * @author marcoreis
 *
 */
public class ProcessadorWikipediaStax {
  private int quantidade; //1.459.849 artigos em 24/12/2011

  public static void main(String[] args) {
    new ProcessadorWikipediaStax().processaArquivo();
  }

  private void processaArquivo() {
    try {
      XMLInputFactory inputFactory = XMLInputFactory.newInstance();
      InputStream in = new FileInputStream(Constantes.ARQUIVO_PAGINAS_WIKIPEDIA);
      XMLStreamReader reader = inputFactory.createXMLStreamReader(in);
      processaDados(reader);
      System.out.println(quantidade);
      reader.close();
    } catch (Exception e) {
      System.out.println(quantidade);
      System.out.println(e.getLocalizedMessage());
    }
  }

  private void processaDados(XMLStreamReader reader) throws XMLStreamException,
      IOException {
    File arquivo = new File(Constantes.INDICE_WIKIPEDIA_STAX);
    if (arquivo.exists()) {
      arquivo.delete();
    }
    FileWriter writer = new FileWriter(arquivo);
    while (reader.hasNext()) {
      TextoWikipedia texto = new TextoWikipedia();
      //
      processaPagina(reader, texto);
      processaRevisao(reader, texto);
      processaTexto(reader, texto);
      writer.write(texto.getPageId() + "^" + texto.getTitle() + "\n");
      //      if (quantidade % 10000 == 0)
      //        System.out.println("Registros: " + quantidade);
      quantidade++;
    }
    writer.close();
  }

  private void processaPagina(XMLStreamReader reader, TextoWikipedia texto) {
    try {
      StaxUtil.moveReaderToElement("page", reader);
      StaxUtil.moveReaderToElement("title", reader);
      String title = reader.getElementText();
      texto.setTitle(title);
      StaxUtil.moveReaderToElement("id", reader);
      String id = reader.getElementText();
      System.out.println(id + "^" + title);
      texto.setPageId(id);
    } catch (XMLStreamException e) {
      e.printStackTrace();
    }
  }

  private void processaRevisao(XMLStreamReader reader, TextoWikipedia texto) {
    try {
      //      StaxUtil.moveReaderToElement("revision", reader);
      //      StaxUtil.moveReaderToElement("id", reader);
      //      texto.setRevisionId(reader.getElementText());
      StaxUtil.moveReaderToElement("timestamp", reader);
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
      try {
        Date time = format.parse(reader.getElementText());
        Timestamp timeStamp = new Timestamp(time.getTime());
        texto.setTimeStamp(timeStamp);
      } catch (ParseException e) {
        e.printStackTrace();
      }
      StaxUtil.moveReaderToElement("username", reader);
      texto.setUserName(reader.getElementText());
    } catch (XMLStreamException e) {
      e.printStackTrace();
    }
  }

  private void processaTexto(XMLStreamReader reader, TextoWikipedia texto) {
    try {
      StaxUtil.moveReaderToElement("text", reader);
      String conteudo = reader.getElementText();
      texto.setText(conteudo);
    } catch (XMLStreamException e) {
      e.printStackTrace();
    }
  }
}
